#include "CFrameApp.h"
#include "Windows.h"
CFrameApp::CFrameApp()
{
    this->_clock = clock_t();
}

void CFrameApp::onload()
{
    this->setTimer(IDT_KEYLISTENER, 0, 1, nullptr);

}

void CFrameApp::onTimer(ID ctid)
{
    switch (ctid)
    {
    case IDT_KEYLISTENER:
        
    default:break;
    }
}

void CFrameApp::setTimer(ID ctid, clock_t timeout, clock_t interval, void(*func)())
{
    this->_timers.at(ctid).timeout = timeout;
    this->_timers.at(ctid).interval = interval;
    this->_timers.at(ctid).onTimer = func;
}

void CFrameApp::clearTimer(ID ctid)
{
    this->_timers.erase(ctid);
}

void CFrameApp::timer()
{
    while (this->_timers.size())
    {
        for (auto iter = this->_timers.begin(); iter != this->_timers.end();)
        {
            iter->second.timeout--;
            if (iter->second.timeout == 0)
            {
                if (iter->second.onTimer)iter->second.onTimer();
                else this->onTimer(iter->first);
                if (iter->second.interval == 0)
                {
                    iter = this->_timers.erase(iter);
                    continue;
                }
                else
                    this->_timers.at(iter->first).timeout = iter->second.interval;
            }
            iter++;
        }
        Sleep(1);
    }
}
